package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.*;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.db.SettingsApi;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.SettingsVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.security.PrivateKey;

@Service
public class SettinsService {


    @Reference
    private QuestionApi questionApi;
    @Reference
    private SettingsApi settingsApi;
    @Reference
    private BlackListApi blackListApi;


    public ResponseEntity querySettings() {
        SettingsVo settingsVo = new SettingsVo();

        User user = UserHolder.getUser();

        Question question = questionApi.findByUserId(user.getId());
        if(question!=null){
            settingsVo.setStrangerQuestion(question.getTxt()); //陌生人问题  从Question对象中
        }
        Settings settings = settingsApi.findByUserId(user.getId());
        if(settings!=null){
           /* settingsVo.setGonggaoNotification(settings.getGonggaoNotification());  //Settings对象中，
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());*/
            BeanUtils.copyProperties(settings,settingsVo);
        }
        settingsVo.setPhone(user.getMobile());
        return ResponseEntity.ok(settingsVo);
    }

    public ResponseEntity blacklist(Integer page, Integer pagesize) {

        IPage<UserInfo> iPage = blackListApi.blacklistByUserId(page,pagesize,UserHolder.getUser().getId());
//        Integer page,Integer pagesize,int counts,List list
      return   ResponseEntity.ok(new PageResult(page,pagesize,(int)iPage.getTotal(),iPage.getRecords()));
    }

    public ResponseEntity delBlacklist(Long blackListId) {
        Long userId = UserHolder.getUserId();
//        delete from tb_black_list where user_id=? and black_list_id=?
        blackListApi.delBlacklist(userId,blackListId);
        return ResponseEntity.ok(null);
    }

    public ResponseEntity saveQuestions(String content) {
//        保存的是某个人的陌生人问题
        Long userId = UserHolder.getUserId();
//        查询某个人的陌生人问题
        Question question = questionApi.findByUserId(userId);
//        做判断 这个人之前是否设置过陌生人问题，如果设置过就是修改，如果没有设置过就是新增
        if(question!=null){
            question.setTxt(content);
            questionApi.update(question);
        }else{
            question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.save(question);
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity updateNotification(Boolean like, Boolean pinglun, Boolean gonggao) {
        Long userId = UserHolder.getUserId();
//        保存的是某个人的 通知设置
 //        查询某个人的通知设置
        Settings settings = settingsApi.findByUserId(userId);

//        做判断 这个人之前是否设置过 “通知设置”，如果设置过就是修改，如果没有设置过就是新增
        if(settings!=null){
            settings.setLikeNotification(like);
            settings.setPinglunNotification(pinglun);
            settings.setGonggaoNotification(gonggao);
            settingsApi.update(settings);
        }else{
            settings = new Settings();
            settings.setLikeNotification(like);
            settings.setPinglunNotification(pinglun);
            settings.setGonggaoNotification(gonggao);
            settings.setUserId(userId);
            settingsApi.save(settings);
        }
        return ResponseEntity.ok(null);
    }
}
